﻿/*
    创 建 人     ： 陈 锐
    创 建 时 间  ： 2012.02.10
*/
(function () {
    $.extend($.fn, {
        daanMap: function (option) {
            var setting = $.extend({
                //默认北京市
                center: new BMap.Point(116.403981, 39.914492),
                zoom: 12,
                autoZoom: false,
                searchReusable: false,
                autoCenter: true,
                geoload: function () { },
                load: function () { }
            }, option);
            return daanMapConstructor(this, setting);
        }
    });
    function daanMapConstructor(mapElt, option) {
        var map = new BMap.Map(mapElt[0]);
        map.addControl(new BMap.NavigationControl());
        map.enableScrollWheelZoom();

        if (!option.autoZoom) {
            map.setZoom(option.zoom);
        }

        //map.addEventListener("load", mapload);

        map.oldCenterAndZoom = map.centerAndZoom;

        map.centerAndZoom = changeCenter;

        var localAddress = null;

        var localPoint = null;

        var searchs = [];

        var search = null;



        var center = null;

        var geo = new BMap.Geocoder();

        if (option.autoCenter) {
            $.getScript("http://j.maxmind.com/app/geoip.js", function () {
                try {
                    localPoint = new BMap.Point(geoip_longitude(), geoip_latitude());
                    loadMap(localPoint, option.zoom, true);
                }
                catch (e) {

                }
            });
        }
        else {
            localPoint = option.center;
            loadMap(localPoint, option.zoom, false);
        }

        function loadMap(centerPoint, zoom, geoip) {
            geo.getLocation(centerPoint, function (e) {
                if (e) {
                    localAddress = e.addressComponents;
                    map.setCurrentCity(e.addressComponents.city);
                    map.centerAndZoom(centerPoint, zoom);
                    if (geoip) {
                        if (typeof option.geoload == "function") {
                            option.geoload.call(mapapi);
                        }
                    }
                }
            });
        };

        function getLocalAddress() { return localAddress; };
        function getLocalPoint() { return localPoint; };
        function mapload() {
            if (typeof option.load == "function") {
                option.load.call(mapapi);
            }
        };

        function changeCenter(center, zoom) {
            geo.getLocation(center, function (e) {
                if (e) {
                    localAddress = e.addressComponents;
                    map.setCurrentCity(e.addressComponents.city);
                    map.oldCenterAndZoom(center, zoom);
                    mapload();
                }
            });
        };

        function setSearch(s) {
            if (option.searchReusable) {
                for (var i = 0; i < searchs.length; i++) {
                    if (searchs[i].getCity() == s.getCity()) {
                        search.stop();
                        search = searchs[i];
                        search.search();
                        return false;
                    }
                }

                search && search.stop();
                search = s;
                search.search();
                searchs.push(s);
            }
            else {
                search && search.stop();
                search = s;
                search.search();
            }
        };

        function getSearch() {
            return search;
        };

        function getGeocoder() {
            return geo;
        }

        var mapapi = { map: map,
            getLocalAddress: getLocalAddress,
            getLocalPoint: getLocalPoint,
            changeCenter: changeCenter,
            setSearch: setSearch,
            getSearch: getSearch,
            getGeocoder: getGeocoder
        };

        return mapapi;
    };
})();